package com.example.androidaudiovideodemo

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.CheckBox
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.androidaudiovideodemo.utils.VideoSplitBean

/**
 *
 * author: renbing
 *
 * date: 2022/1/5
 *
 * des:
 */
class VideoAudioListAdapter(val context: Context, val datas: List<VideoSplitBean>,val itemClick: (position: Int,data: VideoSplitBean) ->Unit,val itemLongClick: (position: Int,data: VideoSplitBean) ->Boolean) : RecyclerView.Adapter<VideoAudioListAdapter.ViewHolder>(){

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_audio_list,parent,false))
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.line.visibility = if (position == datas.size-1) View.GONE else View.VISIBLE
        holder.name.text = datas[position].name
        holder.size.text = datas[position].size
        holder.time.text = datas[position].time
        holder.selected.isChecked = datas[position].isSelected

        holder.itemView.setOnClickListener {
            itemClick.invoke(position,datas[position])
        }

        holder.itemView.setOnLongClickListener {
            itemLongClick.invoke(position,datas[position])
        }
    }

    override fun getItemCount() = datas.size

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView){
        val name = itemView.findViewById<TextView>(R.id.item_name)
        val size = itemView.findViewById<TextView>(R.id.item_size)
        val time = itemView.findViewById<TextView>(R.id.item_time)
        val selected = itemView.findViewById<CheckBox>(R.id.item_select)
        val line = itemView.findViewById<View>(R.id.item_line)
    }
}